package cn.cagurzhan.service;

import cn.cagurzhan.domain.R;
import com.alibaba.dashscope.aigc.conversation.Conversation;
import com.alibaba.dashscope.aigc.conversation.ConversationParam;
import com.alibaba.dashscope.aigc.conversation.ConversationResult;
import com.alibaba.dashscope.aigc.multimodalconversation.*;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * AI辅助灌溉决策
 * @author AjaxZhan
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class LlmService {

    private static final String apiKey = "";

    private final CacheService cacheService;

    /**
     * 语言大模型决策
     * @param deviceInfo 设备信息
     * @return 决策信息
     */
    public R<Object> askAI(R<Map<String,Object>> deviceInfo) throws NoApiKeyException, InputRequiredException {
        Constants.apiKey=apiKey;
        Conversation conversation = new Conversation();
        String prompt = "你是一个农业灌溉的专家，我将给你一段包含土壤湿度、空气湿度、土壤温度、空气温度、紫外线强度、土壤PH、土壤导电率等信息的JSON数据，" +
                "请你根据这些数据进行思考，返回给我一个合适的农业灌溉方案。注意，返回的格式是针对这份数据的具体方案，不要宽泛的答案，不要给出代码。数据如下：\n" +
                JsonUtils.toJson(deviceInfo.getData());
        ConversationParam param = ConversationParam
                .builder()
                .model("qwen-1.8b-chat")
                .prompt(prompt)
                .build();
        ConversationResult result = conversation.call(param);

        log.info(JsonUtils.toJson(result));
        return R.ok(null,result.getOutput().getText().toString());
    }

    /**
     * 视觉大模型决策
     * @param address 图片地址
     */
    public R<Object> askVis(String address) throws NoApiKeyException, UploadFileException {
        Constants.apiKey="";
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessageItemImage userImage = new MultiModalMessageItemImage(address);
        MultiModalMessageItemText userText = new MultiModalMessageItemText("你是一位农业诊断专家，请你根据图片判断描述这个农产品的情况，" +
                "再你描述的状态的基础上，给出这个农产品的灌溉意见");
        MultiModalConversationMessage userMessage =
                MultiModalConversationMessage.builder().role(Role.USER.getValue())
                        .content(Arrays.asList(userImage, userText)).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model(MultiModalConversation.Models.QWEN_VL_CHAT_V1)
                .message(userMessage).build();
        MultiModalConversationResult result = conv.call(param);
        log.info(result.toString());
        cacheService.set("vis",result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString());
        return R.ok(null,result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString());
    }
}
